/*
 * $Id: eth.c 114 2011-01-07 12:33:44Z hyunghwan.chung $
 *
   Copyright 2005-2009 Chung, Hyung-Hwan.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an  BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */

#include <xp/net/eth.h>
#include <xp/net/sck.h>

xp_eth_t* xp_eth_open (const char* dev)
{
	xp_eth_t* eth;

	eth = (xp_eth_t*) xp_malloc (XP_SIZEOF(xp_eth_t));
	if (eth == XP_NULL) return XP_NULL;

	eth->hnd = xp_sckopen (PF_PACKET, SOCK_RAW, xp_hton16(ETH_P_ALL));
	if (eth->hnd == -1)
	{
		xp_free (eth);
		return XP_NULL;
	}

	xp_scksetcloexec (eth->hnd, 1);

	xp_mstrxcpy (ifr.ifr_name, xp_countof(ifr.ifr_name), device);
	if (ioctl(eth->hnd, SIOCGIFINDEX, &ifindex) == -1)
	{
		xp_free (eth);
		return XP_NULL;
	}

	eth->sll.sll_family = AF_PACKET;
	eth->sll.sll_ifindex = ifindex;
	return eth;
}

int xp_eth_close (xp_eth_t* eth)
{
	xp_free (eth);
	return 0;
}

xp_ssize_t xp_eth_send (xp_eth_t* eth, const void* msg, xp_size_t len)
{
	return xp_scksendto (eth->hnd, msg, len, 0, 
		(struct sockaddr*)&eth->sll, XP_SIZEOF(eth->sll));
}

/*
int xp_eth_setadr ()
{
}
int xp_eth_getadr ()
{
}
*/
